<?php
/*
    @name       Data Frolic
    @version    0.1
    
    @for        MODx Evolution 1.0.2
    
    @author     Peter Ehrlich - www.pehrlich.com
    
    @DESCRIPTION
    Used to edit database tables associated with a document within the document edit page.

	@DEPENDENCIES:
	managermanager
    
    @INSTALLATION:
	
	@LICENSE:
	(c) Peter Ehrlich Equals Win Inc
	Released under the GNU General Public License: http://creativecommons.org/licenses/GPL/3.0/
	
*/


define('MODX_MANAGER_PATH', '../../../manager/');
include_once("core.inc.php");
include_once("make_tables.php");
include_once("make_forms.php");
include_once('feed.inc.php');



// Check the current event
global $e;
$e = &$modx->Event;

// The start of adding or editing a document (before the main form)
switch ($e->name) {
	case 'OnPluginFormRender':
	
	$e->output('<h2>OnPluginFormRender Test from DataFrolic</h2>');
	break;
	
	case 'OnDocFormPrerender':
		 $o .= "<!--     begin datafrolic prerender output   -->\n";
		$o .= '<link rel="stylesheet" type="text/css" href="'.$modx->config['base_url']."assets/plugins/dataFrolic/styling/dataFrolic.css\">\n";
		$o .= '<script type="text/javascript" src="'.$modx->config['base_url'].'assets/plugins/dataFrolic/jscripts/dataFrolic.js"></script>';
		$o .= '<script type="text/javascript" src="'.$modx->config['base_url'].'assets/plugins/dataFrolic/jscripts/site.js"></script>';
		$o .= '<script type="text/javascript" src="'.$modx->config['base_url'].'assets/plugins/dataFrolic/jscripts/jquery.color.js"></script>'; //animate background colors
		$o .= '<script type="text/javascript" src="'.$modx->config['base_url'].'assets/plugins/dataFrolic/jscripts/jquery.color.js"></script>';
		ob_start();
		include('modal_box.inc.php');
		$o .= ob_get_contents();
		ob_clean();
		$o .= '<!--     end datafrolic prerender output     -->';
		$e->output($o);		
		
	break;
	
	case 'OnDocFormRender':

		//DEMO CONTENT:
		//TODO: set up phpdoc
		//arguments are:
		
		// A one-to-many relationship
		//function df_createO2MTab($plural_name, $singular_name, &$table_info, $current_doc_id, $roles='', $templates='', $doc='', $plugin_cell=''){
		
		// A more complex mny-to-many relationship
		//function df_createM2MTab($plural_name, $singular_name, &$table_info, $current_doc_id, $roles='', $templates='', $doc=''){

		// names are as displayed in tab title, headings, etc.
		// table info is the portable class which is the model for a database.  Silly, I know.
		// current doc id is resource id for the active resource which has this datafrolic tab.
		// roles, templates, doc, are as defined by managermanager: which of those do you want this rule applied for
		
		//class df_table_info{
		//	function __construct($table, $foreign_keys='',$summary_fields=''){
		
		// $table: MYSQL table name
		// $foreign_keys: links either two tables together for datafrolic (through a db column) or associates a db column with a MODx parent resource.  For an M2M the first value in this array is used to specify the column name of the primary one of the joined items.  
		// $summary_fields: Assoc array of MYSQL field names => Field Display titles for use in the markup etc.  Can have 0-unlimited entries or be empty
		
		
		$office_tpl = '6';
		$news1 = '6';$news2 = '16';	
		$current_doc_id = $e->params['id'];
		
		//news articles for news
		$articles = new df_table_info('news', '', array('date' => 'Article Date', 'title' => 'Title', 'status' => 'Active?') );
		$articles->setPrimaryKey('id');  //hides the field in the view
	
		$plugin_cell = '$tdData = make_publish_links($article_id);';
		df_createO2MTab('Articles', 'Article', $articles, $current_doc_id,'','',$news1, $plugin_cell); //todo: allow multiple
		df_createO2MTab('Articles', 'Article', $articles, $current_doc_id,'','',$news2, $plugin_cell);
		
		//contacts for offices
		$contacts = new df_table_info('contacts', array('office_id' => '22'), array('name' => 'Name', 'email' => 'E-mail', 'speciality' => 'Speciality', 'status' => 'Active?') );
		df_createO2MTab('Contacts', 'Contact', $contacts, $current_doc_id,'',$office_tpl,'');
			
		//create the M2M products table for an office:
		// $LOBs = new df_table_info('')
		$products = new df_table_info('products', array('LOB_id' => '30'), array('title' => 'Product Name', 'LOB_id' => 'Line of Business') );  //prdoucts have no foreign key
		$products->setPrimaryKey('productID'); //links the tables
			
		$productTypes = new df_table_info('productTypes', '', array('title' => 'Product Type'));  //types have no foreign key
				
		$OPJoin = new df_M2M_table_info('officeProduct', array('officeId', 'productId' => &$products, 'productTypeId' => &$productTypes));
		$OPJoin->setPrimaryKey('officeProductId');
		df_createM2MTab('Products', 'Product', $OPJoin, $current_doc_id, '', $office_tpl);
		
		
		//create M2M Contacts for a state:
		$state_tpl = 11;
		$contacts->setPrimaryKey('id');
		$SCJoin = new df_M2M_table_info('stateContact', array('stateId', 'contactId' => $contacts));
		df_createM2MTab('Contacts', 'Contact', $SCJoin, $current_doc_id, '', $state_tpl);
		
		
		//create O2M Markets
		$coverages = new df_table_info('coverage');
		$coverages->setPrimaryKey('id'); //unnecessary as id is default
		//may be error here for no associated [0] array
		$markets = new df_table_info('markets', array('coverageId' => $coverages), array('name' => 'Name', 'coverageId' => 'Coverage'));
		
		//children set summary fields:
		$coverageTypes = new df_table_info('coverageTypes', '', array('title' => 'Coverage Type'));
		$coverageTypes->setPrimaryKey('id');
		
		//create M2M Markets for a state:
		$SMJoin = new df_M2M_table_info('stateMarket', array('stateId', 'marketId' => &$markets, 'coverageTypeId' => &$coverageTypes));
		df_createM2MTab('Markets', 'Market', $SMJoin, $current_doc_id, '', $state_tpl);
		
	break;
	
}


?>